package com.woniuxy.shop.servelet.manager;

import java.io.IOException;
import java.util.List;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.woniuxy.shop.entity.CateGory;
import com.woniuxy.shop.exception.CannotDeleteCateGoryException;
import com.woniuxy.shop.exception.SameCateGoryException;
import com.woniuxy.shop.exception.ServiceException;
import com.woniuxy.shop.service.CateGoryService;
import com.woniuxy.shop.service.ServiceProxyFactory;

/**
 * Servlet implementation class CateGoryServelet
 */
@WebServlet("/manager/CateGoryServelet")
public class CateGoryServelet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CateGoryService cgs = ServiceProxyFactory.getProxy(CateGoryService.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");// 获取参数前指定编码
		String opr = request.getParameter("opr");
		if ("add".equals(opr)) {
			doAdd(request, response);
		} else if ("query".equals(opr)) {
			doQuery(request, response);
		} else if ("initUpdate".equals(opr)) {
			doInitUpdate(request, response);
		} else if ("update".equals(opr)) {
			doUpdate(request, response);
		}else if("del".equals(opr)) {
			doDel(request,response);
		}
	}

	private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		int id = Integer.parseInt(request.getParameter("id"));
		String name="";
		try {
			name=cgs.getCateGoryById(id).getName();
			cgs.delete(id);
		} catch (ServiceException e) {
			if(e.getCause() instanceof CannotDeleteCateGoryException) {
				request.setAttribute("msg", "该类别已有商品，不能被删除");
			}else {
				request.setAttribute("msg", "系统维护中。。。");
			}
			e.printStackTrace();
			List<CateGory> categories = cgs.getCateGory();
			request.setAttribute("categories", categories);
			request.getRequestDispatcher("category/category_list.jsp").forward(request, response);
			return;
		}
		request.setAttribute("success", name+"删除成功");
		request.getRequestDispatcher("CateGoryServelet?opr=query").forward(request, response);
	}

	private void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String status = request.getParameter("status");
		if (name == null || name.trim() == "") {
			request.setAttribute("msg", "商品类别信息不能为空");
			request.getRequestDispatcher("category/category_update.jsp").forward(request, response);
			return;
		}
		if (name.length() > 10) {
			request.setAttribute("msg", "商品类别名字不能超过10位");
			request.getRequestDispatcher("category/category_update.jsp").forward(request, response);
			return;
		}

		try {
			CateGory cg = cgs.getCateGoryById(id);
			if(!name.equals(cg.getName())) {
				cg.setName(name);
			}
			cg.setStatus(status);
			cgs.update(cg);
			
		} catch (ServiceException e) {
			if(e.getCause() instanceof SameCateGoryException) {
				request.setAttribute("msg", "类别"+name+"已存在");
				
				request.getRequestDispatcher("category/category_update.jsp").forward(request, response);
			}
			request.setAttribute("msg", "系统维护，请稍后再试");
			request.getRequestDispatcher("category/category_update.jsp").forward(request, response);
			return;
		}
		request.setAttribute("success", name+"更新成功");
		request.getRequestDispatcher("category/category_update.jsp").forward(request, response);
//		response.sendRedirect("CateGoryServelet?opr=query");
//		request.getRequestDispatcher("CateGoryServelet?opr=query").forward(request, response);
	}


	private void doInitUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		CateGory cg = new CateGory();
		int id = Integer.parseInt(request.getParameter("id"));
		try {
			CateGory cg = cgs.getCateGoryById(id);
			request.setAttribute("category", cg);
//			response.sendRedirect(arg0);
//			System.out.println(cg);
			request.getRequestDispatcher("category/category_update.jsp").forward(request, response);
		} catch (ServiceException e) {
			System.out.println(e.getMessage());
		}

	}

	private void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<CateGory> categories = cgs.getCateGory();// 查询
		request.setAttribute("categories", categories);// 将查询到的数据存储到request的作用域
		request.getRequestDispatcher("category/category_list.jsp").forward(request, response);
		// 后台转发
	}

	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		CateGory cg = new CateGory();
		String name = request.getParameter("name").trim();
		if (name != null && name != "") {
			cg.setName(name);
			cg.setStatus("正常");
		} else {
			request.setAttribute("msg", "亲，商品类别信息不能为空哦");
			request.getRequestDispatcher("category/category_add.jsp").forward(request, response);
		}
		try {
			cgs.add(cg);
		} catch (ServiceException e) {
//			System.out.println("系统维护中。。。");
			if (e.getCause() instanceof SameCateGoryException) {
				// 提示类别已存在的信息
				request.setAttribute("msg", "亲，不能重复添加哦");
				request.setAttribute("cg", cg);
				request.getRequestDispatcher("category/category_add.jsp").forward(request, response);
				return;
			}
		}
//		response.sendRedirect("index.jsp");
		request.setAttribute("success", "类别"+name+"添加成功");
		request.getRequestDispatcher("category/category_add.jsp").forward(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
